package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FlowerpotMapper;
import com.wjh.pojo.Flower;
import com.wjh.pojo.Flowerpot;
import com.wjh.pojo.FlowerpotType;
import com.wjh.service.FlowerpotService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 花盆(Flowerpot)表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 19:38:34
 */
@Service("flowerpotService")
public class FlowerpotServiceImpl extends ServiceImpl<FlowerpotMapper, Flowerpot> implements FlowerpotService {


    @Resource
    private FlowerpotService flowerpotService;

    @Override
    public ResponseResult queryListFlowerpot(Integer pageNum, Integer pageSize, Integer type, String flowerpotName) {
        LambdaQueryWrapper<Flowerpot> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null,Flowerpot::getType,type);
        queryWrapper.like(flowerpotName != null,Flowerpot::getName,flowerpotName);

        //分页查询
        Page<Flowerpot> certificatePage = new Page<>(pageNum,pageSize);
        Page<Flowerpot> page = page(certificatePage, queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public ResponseResult addFlowerpot(Flowerpot flowerpot) {
        if(flowerpot.getType() == null){
            throw new SystemException(AppHttpCodeEnum.NEED_TYPE);
        }
        if(!StringUtils.hasText(flowerpot.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }
        if(!StringUtils.hasText(flowerpot.getPrice())){
            throw new SystemException(AppHttpCodeEnum.NEED_PRICE);
        }
        if(!StringUtils.hasText(flowerpot.getImage())){
            throw new SystemException(AppHttpCodeEnum.NEED_IMAGE);
        }
        save(flowerpot);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delByIds(List<Integer> id) {
        if(id == null){
            throw new SystemException(AppHttpCodeEnum.ID_NOTNULL);
        }
        LambdaQueryWrapper<Flowerpot> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.in(Flowerpot::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public void delByTypeIds(Integer id) {
            LambdaQueryWrapper<Flowerpot> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Flowerpot::getType,id);
            remove(queryWrapper);
    }

    @Override
    public ResponseResult queryListByType(Integer companyId, Integer typeId) {
        List<Flower> flowerpotList =
                getBaseMapper().queryTypeFlowerpot(companyId, typeId);
        return ResponseResult.okResult(new PageVo(flowerpotList,(long)flowerpotList.size()));
    }

    @Override
    public ResponseResult likeFlowerpot(String name) {
        if(!StringUtils.hasText(name)){
            throw new SystemException(AppHttpCodeEnum.NEED_CONTENT);
        }
        LambdaQueryWrapper<Flowerpot> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Flowerpot::getName,name);
        List<Flowerpot> list = list(queryWrapper);
        return ResponseResult.okResult(new PageVo(list,(long)list.size()));
    }
}
